package com.hao.tool.extra.poi.excel;

import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.util.CellRangeAddressList;

/**
 * Excel表格设置校验条件
 * Created by haol on 2017/4/17.
 */
public class SetValidation {


    public void setDataValidation(HSSFSheet sheet, HSSFCell cell) {
        DVConstraint constraint = DVConstraint.createNumericConstraint(
                DVConstraint.ValidationType.INTEGER, DVConstraint.OperatorType.GREATER_OR_EQUAL, "0", null);
        CellRangeAddressList regions = new CellRangeAddressList(
                cell.getRowIndex(), cell.getRowIndex(), cell.getColumnIndex(), cell.getColumnIndex());
        HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);
        sheet.addValidationData(data_validation);
    }

    public void setProsValidation(HSSFSheet sheet, HSSFCell cell,
                                  int firstRow, int lastRow, int firstCol, int lastCol, String[] pros) {

        CellRangeAddressList regions = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
        DVConstraint constraint = DVConstraint.createExplicitListConstraint(pros);
        HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);
        sheet.addValidationData(data_validation);
    }
}
